A short description of the post.
Important to set fig.retina to be 3 in order for subsequent visualization to be sharp.
packages <- c('maptools','sf','raster','spatstat','tmap','tidyverse')
for (p in packages){
if(!require(p, character.only = T)){
install.packages(p)
}
}
library(p, character.only=T)
The code chunk import shapefile using st_read() of sf package. The output object is in tibble sf object class.
Pgl_sf and mpsz_sf geospatial data sets into RStudio.
Note: - Do not paste directory in dsn
Pgl_sf <- st_read(dsn="data/shapefile", layer="Punggol_St")
Reading layer `Punggol_St' from data source
`C:\Users\User\Desktop\IS415\lye-jia-wei\IS415_blog\_posts\2021-09-13-in-class-exercise-5\data\shapefile'
using driver `ESRI Shapefile'
Simple feature collection with 2642 features and 2 fields
Geometry type: LINESTRING
Dimension: XY
Bounding box: xmin: 34038.56 ymin: 40941.11 xmax: 38882.85 ymax: 44801.27
Projected CRS: SVY21 / Singapore TM
mpsz_sf <- st_read(dsn="data/shapefile",layer="MP14_SUBZONE_WEB_PL")
Reading layer `MP14_SUBZONE_WEB_PL' from data source
`C:\Users\User\Desktop\IS415\lye-jia-wei\IS415_blog\_posts\2021-09-13-in-class-exercise-5\data\shapefile'
using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21
Projection is SVY21
read_rds() of readr package is used instead of readRDS() of base R is used. This is because output of read_rds() is in tibble object.
Note:
No need to set path
Note that there are some data issue in childcare data frame with the Lat and Lng column, the columns should be in numeric data type. The coordinate fields are in decimal degrees. Hence, wgs referencing system is assumed.
childcare <- read_rds("data/rds/childcare.rds")
CHAS <- read_rds("data/rds/CHAS.rds")
Note:
st_as_sf() accept coordinates in character data type.
No significant difference whether coordinate data type is change
Alpha is to set transparency
tmap_mode("view")
tm_shape(childcare_sf)+tm_dots(alpha= 0.4, col="blue", size=0.05)+ tm_shape(CHAS_sf)+tm_dots(alpha= 0.4, col="red", size=0.05)
Note:
Polygon will be converted so spatial polygon object, point will be converted to spatial point.
childcare <- as_Spatial(childcare_sf)
CHAS <- as_Spatial(CHAS_sf)
mpsz <- as_Spatial(mpsz_sf)